#include "ServerSession.h"


CServerSession::CServerSession(asio::io_service& io_service)
	: m_socket(io_service)
	, m_recv_times(0)
{
}


CServerSession::~CServerSession()
{
}

void CServerSession::start()
{
	m_socket.async_read_some(asio::buffer(m_data, MAX_PACKET_LEN),
		bind(&CServerSession::handle_read, this, asio::placeholders::error,
		asio::placeholders::bytes_transferred));
}

void CServerSession::handle_read(const system::error_code& error, size_t transferred)
{
	if (!error)
	{
		++m_recv_times;
		asio::async_write(m_socket, buffer(m_data, transferred),
			bind(&CServerSession::handle_write, this, asio::placeholders::error));
	}
	else
	{
		delete this;
	}
}

void CServerSession::handle_write(const system::error_code& error)
{
	if ( !error )
	{
		start();
	}
	else
	{
		delete this;
	}
}